package com.dinree.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.base.library.TopBarBaseActivity;
import com.base.library.utils.StringUtils;
import com.dinree.R;
import com.dinree.api.UserApi;
import com.dinree.bean.UserBean;
import com.dinree.databinding.ActivityVerifyPasswordBinding;
import com.jakewharton.rxbinding2.view.RxView;

import java.util.concurrent.TimeUnit;

import static com.base.library.Constant.WINDOW_DURATION;

public class VerifyPasswordActivity extends TopBarBaseActivity {
    private String pwd;
    private ActivityVerifyPasswordBinding activityVerifyPasswordBinding;
    private UserBean userBean;

    @Override
    public int getContentView() {
        return R.layout.activity_verify_password;
    }


    @Override
    public void initContentView(Bundle savedInstanceState) {
        activityVerifyPasswordBinding = getContentViewBinding();
        userBean = realm.findFirst(UserBean.class);
        setTitle("修改手机号码");
        setTopLeftButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        RxView.clicks(activityVerifyPasswordBinding.btnSubmit).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
            submit();
        });

    }

    private void submit() {
        pwd = activityVerifyPasswordBinding.editPwd.getText().toString().trim();
        if (StringUtils.isEmpty(pwd)) {
            showMsg("密码不能为空");
            return;
        }
        if (userBean != null) {
            UserApi.getInstance(mContext).verify_password(userBean.user_id, userBean.user_token, pwd).subscribe(user -> {
                Intent intent = new Intent(mContext, EditOldPhoneActivity.class);
                startActivity(intent);
                finish();
            }, e -> {
                e.printStackTrace();
                showMsg(e.getMessage());
            });
        }
    }
}

